<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

import { getDataCount } from '@/api/system/Statement'
const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      datacount: [
        [79, 52, 200, 334, 390, 330, 220],
        [80, 52, 200, 334, 390, 330, 220],
        [30, 52, 200, 334, 390, 330, 220]
      ],
      // 查询条件
      getMessageForm: {
        statementKey: undefined
      },
      getApplyForm: {
        statementKey: undefined
      },
      getBusinessForm: {
        statementKey: undefined
      }
    }
  },

  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },

  created() {
    this.getBusiDataCount()
    this.getMessageDataCount()
    this.getApplyDataCount()
  },
  methods: {
    // 获取业务数据
    getBusiDataCount() {
      this.getBusinessForm.statementKey = 'approve'
      getDataCount(this.getBusinessForm).then(res => {
        // console.log('res ---------- ', res.data)
        const arr = res.data.map(item => item.dataCount)
        // console.log('arr ---------- ', arr)
        this.datacount[0] = arr
        // console.log('this.datacount[0] ----------', this.datacount[0])
        this.initChart()
      })
    },

    // 获取信息量
    getMessageDataCount() {
      this.getMessageForm.statementKey = 'notice'
      getDataCount(this.getBusinessForm).then(res => {
        const arr = res.data.map(item => item.dataCount)
        // console.log('arr ---------- ', arr)
        this.datacount[1] = arr
        this.initChart()
      })
    },

    // 获取数据量
    getApplyDataCount() {
      this.getApplyForm.statementKey = 'apply'
      getDataCount(this.getApplyForm).then(res => {
        const arr = res.data.map(item => item.dataCount)
        // console.log('arr ---------- ', arr)
        this.datacount[2] = arr
        this.initChart()
      })
    },

    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '业务量',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: this.datacount[0],
          animationDuration
        }, {
          name: '信息量',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          // data: [80, 52, 200, 334, 390, 330, 220],
          data: this.datacount[1],
          animationDuration
        }, {
          name: '数据量',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          // data: [30, 52, 200, 334, 390, 330, 220],
          data: this.datacount[2],
          animationDuration
        }]
      })
    }
  }
}
</script>
